package com.bazinga.contact.controller;


import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.bazinga.contact.model.JobSchedule;
import com.bazinga.contact.util.HibernateUtil;


public class JobScheduleManager extends HibernateUtil {

	public JobSchedule add(JobSchedule jobSchedule) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(jobSchedule);
		session.getTransaction().commit();
		return jobSchedule;
	}
	public JobSchedule delete(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		JobSchedule jobSchedule = (JobSchedule) session.load(JobSchedule.class, id);
		if(null != jobSchedule) {
			session.delete(jobSchedule);
		}
		session.getTransaction().commit();
		return jobSchedule;
	}

	@SuppressWarnings("unchecked")
	public List<JobSchedule> list() {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<JobSchedule> jobSchedule = null;
		try {
			
			jobSchedule = (List<JobSchedule>)session.createQuery("from JobSchedule").list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return jobSchedule;
	}
	
	public List<JobSchedule> getByJobId(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<JobSchedule> jobSchedule = null;
		try {
			
			jobSchedule = (List<JobSchedule>)session.createQuery("from JobSchedule where jobid ="+id).list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return jobSchedule;
	}
}
